﻿using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WpfApp_SmartParking_Base;
using WpfApp_SmartParking_Entities;
using WpfApp_SmartParking_IService;
using WpfApp_SmartParking_Models;
using WpfApp_SmartParking_Service;

namespace WpfApp_SmartParking.ViewModels.Dialog;

public class UserDetailViewModel : DetailViewModelBase
{
    public UserModel User { get; set; } = new UserModel();

    public DelegateCommand? NameTextBoxLostFocusCommand { get; set; }

    private readonly IUserService _userService;
    private readonly IEventAggregator _eventAggregator;

    public UserDetailViewModel(IUserService userService, IEventAggregator eventAggregator)
    {
        _userService = userService;
        _eventAggregator = eventAggregator;

        NameTextBoxLostFocusCommand = new DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(User.Name))
                User.ValidateName();
        });

        SubmitCommand = new DelegateCommand(DoSubmit);

        CancelCommand = new DelegateCommand(() =>
        {
            base.CloseWindow(new DialogResult());
        });
    }

    public override bool CanCloseDialog()
    {
        return base.CanCloseDialog();
    }

    public override void OnDialogClosed()
    {
        base.OnDialogClosed();
    }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        UserModel? userModel = parameters.GetValue<UserModel?>("Detail");
        if (userModel == null)
        {
            User.Id = -1;
            Title = "新增用户";
        }
        else
        {
            Title = "用户详情";
            User.Id = userModel.Id;
            User.Name = userModel.Name;
            User.Phone = userModel.Phone;
            User.Address = userModel.Address;
            User.Email = userModel.Email;
            User.Qq = userModel.Qq;
            User.Gender = userModel.Gender;
        }
    }

    public void DoSubmit()
    {
        if (User.HasErrors)
            return;

        if (User.Id == -1)
        {
            _userService.Insert<SysUser>(new SysUser()
            {
                Name = User.Name,
                Password = "a",
                CompanyId = 1,
                Status = 0,
                Phone = User.Phone,
                Mobile = User.Mobile,
                Address = User.Address,
                Email = User.Email,
                Qq = User.Qq,
                Avatar = "a02.jpg",
                Gender = User.Gender
            });
        }
        else 
        {
            SysUser? sysUser = _userService.Query<SysUser>(it => it.Id == User.Id).FirstOrDefault();
            if (sysUser != null) 
            {
                sysUser.Phone = User.Phone;
                sysUser.Mobile = User.Mobile;
                sysUser.Address = User.Address;
                sysUser.Email = User.Email;
                sysUser.Qq = User.Qq;
                sysUser.Gender = User.Gender;
                _userService.Update<SysUser>(sysUser);
                _userService.Commit();
            }
        }
        _eventAggregator.GetEvent<RefreshUserEvent>().Publish();
        base.CloseWindow(new DialogResult(ButtonResult.OK));
    }
    
}
